package com.app.base.okhttp.callback;

/**
 * Created by HJ on 2015/11/13.
 */

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.app.base.dialog.DialogFactory;
import com.app.base.okhttp.OkHttpClientManager;
import com.app.base.utils.LosNetWork;
import com.google.gson.JsonParseException;
import com.google.gson.internal.$Gson$Types;
import com.squareup.okhttp.Request;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class ResultCallback<T> {

    //显示等待dialog 默认为true
    private boolean showProgress;
    private Context context;
    private DialogFactory dialogFactory;
    private Button retryButton;
    private Request request;
    public Type mType;
    public boolean hasNet;

    public void init(Context context, boolean showProgress) {
        this.context = context;
        this.showProgress = showProgress;
        mType = getSuperclassTypeParameter(getClass());
        dialogFactory = new DialogFactory(((FragmentActivity) context).getSupportFragmentManager());
    }

    public ResultCallback(Context context, boolean showProgress) {
        init(context, showProgress);
    }

    public ResultCallback(Context context, boolean showProgress, Button retryButton) {
        init(context, showProgress);
        this.retryButton = retryButton;
        this.retryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                retryRequest();
            }
        });
    }

    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }

    public void onBefore(Request request) {
        this.request = request;
        if(null != retryButton) retryButton.setVisibility(View.INVISIBLE);
        if (!LosNetWork.haveAvailableNetwork(context)) {
            if(null != retryButton) retryButton.setVisibility(View.VISIBLE);
            Toast.makeText(context, "当前无网络连接，请检查网络", Toast.LENGTH_SHORT).show();
            hasNet = false;
            return;
        } else {
            if (showProgress) {
                dialogFactory.showLoadingFragment("loading");
            }
            hasNet = true;
        }
    }

    public void onAfter() {
        if (showProgress) {
            dialogFactory.dismissLoadingFragment();
        }
    }

    public void inProgress(float progress) {

    }

    public void onError(Request request, Exception e) {
        if(null != retryButton) retryButton.setVisibility(View.VISIBLE);
        if (e instanceof IOException) {
            Toast.makeText(context, "连接服务器超时,请重试", Toast.LENGTH_SHORT).show();
        } else if (e instanceof RuntimeException) {
            Toast.makeText(context, "服务器异常", Toast.LENGTH_SHORT).show();
        } else if (e instanceof JsonParseException) {
            Toast.makeText(context, "解析数据失败", Toast.LENGTH_SHORT).show();
        }
    }

    public abstract void onResponse(T response);

    public abstract void onStatus(String response);

    public void retryRequest() {
        OkHttpClientManager.getInstance().execute(request, this);
    }
}